/**
 * 
 */
package webreg.model.swing;

import java.util.Iterator;
import java.util.Vector;

import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import webreg.model.AgeClassification;
import webreg.model.AgeTable;

/**
 * @author STKNOLL
 *
 */
public class AgeTableListModel extends AgeTable implements ListModel, ListDataListener {
	private Vector<ListDataListener> mListeners;
	
	public AgeTableListModel() {
		super();
		mListeners = new Vector<ListDataListener>();		
	}

	public AgeTableListModel(AgeTable toCopy) {
		super(toCopy);
		mListeners = new Vector<ListDataListener>();
	}

	@Override
	public boolean equals(AgeTable toCompare) {
		return super.equals(toCompare);
	}

	public void addElementAt(int arg0, AgeClassification inTable) {
		super.getTable().add(arg0, inTable);
		intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, arg0, arg0));
	}		
	
	public void editElementAt(int arg0, AgeClassification inTable) {
		super.getTable().set(arg0, inTable);
		contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, arg0, arg0));
	}	
	
	public void removeElementAt(int arg0) {
		super.getTable().remove(arg0);	
		intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, arg0, arg0));
	}			
		
	/* (non-Javadoc)
	 * @see javax.swing.ListModel#addListDataListener(javax.swing.event.ListDataListener)
	 */
	@Override
	public void addListDataListener(ListDataListener arg0) {
		mListeners.add(arg0);
	}

	/* (non-Javadoc)
	 * @see javax.swing.ListModel#getElementAt(int)
	 */
	@Override
	public Object getElementAt(int arg0) {
		return super.getTable().elementAt(arg0);
	}

	/* (non-Javadoc)
	 * @see javax.swing.ListModel#getSize()
	 */
	@Override
	public int getSize() {
		return super.getTable().size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.ListModel#removeListDataListener(javax.swing.event.ListDataListener)
	 */
	@Override
	public void removeListDataListener(ListDataListener arg0) {
		mListeners.remove(arg0);
	}

	@Override
	public void contentsChanged(ListDataEvent e) {
		for(Iterator<ListDataListener> iter = mListeners.iterator(); iter.hasNext(); ) {
			ListDataListener inList = (ListDataListener) iter.next();
			inList.contentsChanged(e);
		}
	}

	@Override
	public void intervalAdded(ListDataEvent e) {
		for(Iterator<ListDataListener> iter = mListeners.iterator(); iter.hasNext(); ) {
			ListDataListener inList = (ListDataListener) iter.next();
			inList.intervalAdded(e);
		}		
	}

	@Override
	public void intervalRemoved(ListDataEvent e) {
		for(Iterator<ListDataListener> iter = mListeners.iterator(); iter.hasNext(); ) {
			ListDataListener inList = (ListDataListener) iter.next();
			inList.intervalRemoved(e);
		}		
	}

}
